package com.alliky.controller;

import com.alliky.pojo.Users;
import com.alliky.pojo.bo.UsersBO;
import com.alliky.service.IUserService;
import com.alliky.common.ServerResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("passport")
public class PassportController {

    @Autowired
    IUserService iUserService;
    @ApiOperation(value = "查询用户名是否已经注册", tags = "用户模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("/usernameIsExist")
    public ServerResponse usernameIsExist(@RequestParam String username) {
        // 1.判断用户名不能为空
        if (StringUtils.isBlank(username)) {
            return ServerResponse.createByErrorMessage("用户名不能为空！");
        }
        // 2.查找注册用户名是否存在
        boolean isExist = iUserService.queryUsernameIsExist(username);

        if (isExist) {
            return ServerResponse.createByErrorMessage("用户名已经存在！");
        }

        //请求成功，用户名没有重复
        return ServerResponse.createBySuccessMessage("该用户名可以注册！");
    }

    @ApiOperation(value = "用户注册", tags = "用户模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "confirmPwd", value = "确认密码", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/regist")
    public ServerResponse regist(UsersBO usersBO) {
        String username = usersBO.getUsername();
        String password = usersBO.getPassword();
        String confirmPwd = usersBO.getConfirmPwd();

        //判断用户名密码必须不为空
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(password) ||
                StringUtils.isBlank(confirmPwd)) {
            return ServerResponse.createByErrorMessage("用户名或密码不能为空！");
        }

        //查询用户名是否存在
        boolean isExist = iUserService.queryUsernameIsExist(username);
        if (isExist) {
            return ServerResponse.createByErrorMessage("用户名已经存在！");
        }
        //判断两次密码是否一致
        if (password.length() < 6) {
            return ServerResponse.createByErrorMessage("密码长度不能小于6位数");
        }

        //密码长度不能少于6位
        if (!password.equals(confirmPwd)) {
            return ServerResponse.createByErrorMessage("两次密码不一致");
        }
        //实现注册
        return iUserService.createUser(usersBO);
    }


    @ApiOperation(value = "用户登录", tags = "用户模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String"),
    })
    @PostMapping("/login")
    public ServerResponse<Users> login(@RequestParam String username, @RequestParam String password){
        if (StringUtils.isBlank(username)||StringUtils.isBlank(password)){
            return ServerResponse.createByErrorMessage("用户名或密码不能为空");
        }
        return iUserService.login(username,password);
    }
}
